package com.probiz.estoresa.order.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.order.OrderDelivery;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.order.service.OrderDeliveryManager;

public class OrderDeliveryController extends GenericStoreAdminController<OrderDelivery> {
    private OrderDeliveryManager orderDeliveryManager = null;

    public void setOrderDeliveryManager(OrderDeliveryManager inMgr) {
        this.orderDeliveryManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(OrderDelivery entity) {
		return entity.getOrderDeliveryName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = orderDeliveryManager;
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "convert_gbk(orderDeliveryName)");
		sortMap.put("2", "convert_gbk(shipperName)");
		sortMap.put("3", "convert_gbk(regionFullName)");
		sortMap.put("4", "convert_gbk(address)");
		sortMap.put("5", "mobilephone");
		sortMap.put("6", "sortOrder");
		sortMap.put("7", "isDefault*-1");//默认的在前面
		sortMap.put("8", "status*-1"); //激活的在前面
		return sortMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, OrderDelivery entity, BindException errors) {
	}
	/**
	 * 保存时，对是否为默认进行处理，保证有唯一一条数据是默认的。
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Override
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 取得Form对应的Model
		OrderDelivery entity = formBackingObject(request);

		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, entity);
			errors = new BindException(binder.getBindingResult());
			// 传给子类的实现，后者可以继续验证和加入错误到errors
			preSave(request, entity, errors);
			if (!errors.hasErrors()) {// 里面如果出错应该抛出异常
				orderDeliveryManager.saveFully(entity);
				postSave(request, entity);
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}

		ModelAndView mav;
		if (errors.hasErrors()) {
			mav = showForm(request, errors);
		} else if (successView != null) {
			mav = getModelAndView(successView, errors.getModel());
		} else {
			mav = getRedirectToActionView("edit", ((BaseObject) entity).getId()
					.toString());
		}
		return mav;
	}
}
